package com.deve.pig.controller.merchant;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.utils.DwzAjaxJsonUtil;
import my.utils.Pagination;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.deve.pig.controller.BaseController;
import com.deve.pig.model.MerchantGoods;
import com.deve.pig.service.merchant.IMerchantGoodsService;

/**
 * @description 商品控制器
 * @author tanyouzhong
 * @date 2014年9月13日 下午6:31:21
 */
@Controller
@RequestMapping("/merchant/goods")
public class MerchantGoodsController extends BaseController {

    @Resource
    private IMerchantGoodsService merchantGoodsService;

    @RequestMapping("/showList")
    public String shownList(Model model, HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> params = super.getRequestParameters(request);
        int pageNum = Pagination.CURRENTPAGE;
        int pageSize = Pagination.PAGESIZE;
        if (params.containsKey("pageNum")) {
            pageNum = Integer.parseInt((String) params.get("pageNum"));
        }
        if (params.containsKey("numPerPage")) {
            pageSize = Integer.parseInt((String) params.get("numPerPage"));
        }
        Pagination page = merchantGoodsService.findPagination(params, pageNum, pageSize);

        model.addAttribute("page", page);

        return "admin/merchant/goods/goodslist";
    }


    @RequestMapping("/showAdd")
    public String showAdd(Model model)
    {
        return "admin/merchant/goods/add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(MerchantGoods goods,
            HttpServletRequest request) throws IOException
    {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        if (goods == null)
        {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
        }
        else
        {
            goods.setCreateTime(new Date());
            goods.setUpdateTime(new Date());
            if(goods.getIsTop() == null)
                goods.setIsTop(false);
            if(goods.getIsPublish() != null && goods.getIsPublish())
                goods.setPublishTime(new Date());
            if (merchantGoodsService.add(goods) < 1)
            {
                json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
                json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "抱歉，服务器故障，请重试");
            }
        }
        return json;
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") int id, HttpServletRequest request) throws IOException
    {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");
        
        if (merchantGoodsService.delete(id) < 1)
        {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "删除失败！");
        }
        return json;
    }

    @RequestMapping("/showUpdate/{id}")
    public String showUpdate(@PathVariable("id") int id, Model model)
    {
        MerchantGoods goods = merchantGoodsService.findById(id);
        model.addAttribute("goods", goods);
        return "admin/merchant/goods/update";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(MerchantGoods goods,
            HttpServletRequest request) throws IOException
    {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        if (goods == null)
        {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
        }
        else
        {
            goods.setUpdateTime(new Date());
            if(goods.getIsTop() == null)
                goods.setIsTop(false);
            if(goods.getIsPublish() != null && goods.getIsPublish())
                goods.setPublishTime(new Date());
            if (merchantGoodsService.update(goods) < 1)
            {
                json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
                json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "抱歉，服务器故障，请重试");
            }
        }
        return json;
    }

    /**
     * @Description: 发布
     */
    @RequestMapping("/publish")
    @ResponseBody
    public Map<String, Object> publish(MerchantGoods goods, HttpServletRequest request)
    {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");
        if (goods == null)
        {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
        }
        else
        {
            goods.setIsPublish(true);
            goods.setPublishTime(new Date());
            goods.setUpdateTime(new Date());
            merchantGoodsService.update(goods);
        }
        return json;
    }
    
    /**
     * @Description: 取消发布
     */
    @RequestMapping("/unPublish")
    @ResponseBody
    public Map<String, Object> unPublish(MerchantGoods goods, HttpServletRequest request)
    {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");
        if (goods == null)
        {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
        }
        else
        {
            goods.setIsPublish(false);
            goods.setUpdateTime(new Date());
            merchantGoodsService.update(goods);
        }
        return json;
    }
    
    /**
     * @Description: 置顶
     */
    @RequestMapping("/top")
    @ResponseBody
    public Map<String, Object> top(MerchantGoods goods, HttpServletRequest request)
    {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");
        if (goods == null)
        {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
        }
        else
        {
            goods.setIsTop(true);
            goods.setUpdateTime(new Date());
            merchantGoodsService.update(goods);
        }
        return json;
    }
    
    /**
     * @Description: 取消置顶
     */
    @RequestMapping("/unTop")
    @ResponseBody
    public Map<String, Object> unTop(MerchantGoods goods, HttpServletRequest request)
    {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");
        if (goods == null)
        {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
        }
        else
        {
            goods.setIsTop(false);
            goods.setUpdateTime(new Date());
            merchantGoodsService.update(goods);
        }
        return json;
    }

    /**
     * @description 商品详情
     */
    @RequestMapping("/detail/{id}")
    public String detail(@PathVariable("id") int id, Model model) throws IOException
    {
        MerchantGoods goods = merchantGoodsService.findById(id);
        model.addAttribute("goods", goods);
        return "admin/merchant/goods/detail";
    }

    /**
     * @description 查看大图
     */
    @RequestMapping("/image")
    public String detail() throws IOException
    {
        return "admin/merchant/goods/image";
    }
}
